package com.wzdigit.wms.raw.service.backtosap;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ctc.wstx.util.DataUtil;
import com.ctc.wstx.util.StringUtil;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.basic.Sku;
import com.wzdigit.wms.basic.domain.basic.Warehouse;
import com.wzdigit.wms.basic.domain.mapping.CodeMapping;
import com.wzdigit.wms.basic.domain.mid.IfPostback;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.enums.writeback.BackResultEnum;
import com.wzdigit.wms.basic.enums.writeback.OtherSystemEnum;
import com.wzdigit.wms.basic.enums.writeback.TopicEnum;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.mapper.core.mapping.CodeMappingMapper;
import com.wzdigit.wms.basic.service.code.CodeMappingService;
import com.wzdigit.wms.basic.util.StringUtils;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.raw.service.mid.IfPostbackService;
import com.wzdigit.wms.web.service.wms2sap.checkinstore.ZDT_065_ReqRECORDS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
    * 判检入库信息回写
    * @author shuaic
    * @date 2021/6/18 18:44
*/
@Service
@Slf4j
public class CheckInStoreService {
    @Autowired
    IfPostbackService ifPostbackService ;

    @Autowired
    private CodeMappingService codeMappingService;

    @Autowired
    SkuMapper skuMapper ;

    @Autowired
    CodeMappingMapper codeMappingMapper ;

    @Autowired
    WarehouseMapper warehouseMapper ;

    /*
        * @param type 检验类型 1判检 2 改判
    	* @param status 检验结果 1合格 2 不合格
    	* @param batch 检验批次 SAP返回给我们的批次号
    	* @param userName 检验人
        * @return void
        * @author shuaic
        * @date 2021/8/4 20:58
    */

    public  void  backToSap(String type, String status, AsnDtl asnDtl, String userName, String asnNo, String remark,String oqcNO) throws Exception{
        List<ZDT_065_ReqRECORDS>  list = new ArrayList<>();
        ZDT_065_ReqRECORDS record = new ZDT_065_ReqRECORDS();
        record.setQCTYPE(type); //业务类型 1 判检 2 改判
        record.setVCODE(status);  //状态  1 合格 2 不合格
        record.setQPLOS(asnDtl.getUdf4());  //检验批
        record.setUNAME(userName);  //创建人
        record.setDATUM(DateUtil.format(new Date(),"yyyyMMdd"));  //创建时间
        record.setZBUHG("100");//不合格的时候要传不然会报错
        record.setRSV1(asnDtl.getAsnDtlId()+"");//wmsId 传asnDtlId
        //和入库保持一致(检验单号+'-'+入库明细ID);
        record.setRSV2(oqcNO+"-"+asnDtl.getAsnDtlId());//检验批 传IQC检验单号
        if(StringUtils.isNotNull(remark)){
            record.setZTXT(remark);
        }else {
            record.setZTXT("无备注");
        }


        list.add(record);
        String jsonString = JSON.toJSONString(list);
        log.info("SAP判检入库报文===》"+jsonString);
        //插入数据到待回传数据表
        IfPostback ifPostback = new IfPostback();
        ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
        ifPostback.setPostbackTopic(TopicEnum.TOPIC_SAP_CHECK_IN_STORE.getEnumName());
        ifPostback.setPostbackNo(asnNo);
        ifPostback.setPostbackContent(jsonString);
        ifPostback.setPostbackResult(BackResultEnum.DEFAULT_STATE.getCode());
        ifPostback.setCreateTime(new Date());
        ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
        try {
            ifPostbackService.addOne(ifPostback);//保存
        } catch (Exception e) {
            e.printStackTrace();
            Shift.fatal(StatusCode.SERVER_MODIFY_ERROR);
        }
    }


}
